<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>演示用户名是否已被注册</title>
<script type="text/javascript" src="JQuery/jquery-1.11.1.js"></script>
<script type="text/javascript">
//创建XMLHttpRequest异步对象
function createXMLHttpRequest() {
	try {
		return new XMLHttpRequest();     //支持大多数浏览器
	} catch (e) {
        try {
			return new ActiveXObject("Msxml2.XMLHTTP");   //支持IE6.0
		} catch (e) {
			try {
				return new ActiveXObject("Microsoft.XMLHTTP");  //支持IE5.5及以下版本
			} catch (e) {
				throw e;
			}
		}
	}
}

$(function() {
	$("#name").blur(function() {
		alert("1");
		var xmlHttp = createXMLHttpRequest();
		xmlHttp.open("POST", "/ajax_project/ValidateUsername", true);
		xmlHttp.setRequestHeader("Content-Type", "application-x-www-form-urlencoded")
		xmlHttp.send("username=" + $("#name").val());
		xmlHttp.onreadystatechange = function() {
			if(xmlHttp.readyState == 4 && xmlHttp.status == 200) {
				var text = xmlHttp.responseText;
				var errorSel = $("#error");
				if(text == "1") {
					errorSel.html("用户名已注册！");
				} else {
					errorSel.html("");
				}
			}
		}
		
	});
})
</script>
</head>
<body>
<h1>演示用户名是否已被注册</h1>
<form action="/ajax_project/AjaxServlet" method="POST">
    用户名：<input type="text" id="name" name = "username"><span id="error"></span><br><br>
   密  &nbsp; 码：<input type="password" name="password"><br><br>
   <input type="submit" value="注册">
</form>
</body>
</html>